package com.zh.note.huawei.onehundred.射击比赛or统计射击比赛成绩;

/**
 * @Author: zhanghuan
 * @date: 2023/9/4 12:35
 * @description: create it
 * <p>
 * 13
 * 3,3,7,4,4,4,4,7,7,3,5,5,5
 * 53,80,68,24,39,76,66,16,100,55,53,80,55
 */

import java.util.*;


public class Main {
    public static void main(String[] args) {
        try {
            Scanner scan = new Scanner(System.in);
            // int n = Integer.parseInt(scan.nextLine());
            int n = 13;
            if (n < 2 || n > 100) {
                System.out.println("input error");
                return;
            }
            // String idStr = scan.nextLine();
            String idStr = "3,3,7,4,4,4,4,7,7,3,5,5,5";
            if (n < 2 || n > 100) {
                System.out.println("input error");
                return;
            }
            // String score = scan.nextLine();
            String score = "53,80,68,24,39,76,66,16,100,55,53,80,55";
            String[] ids = idStr.split(",");
            String[] scoreArray = score.split(",");

            Map<Integer, List<Integer>> map = new HashMap<>();

            for (int j = 0; j < n; ++j) {
                int idKey = Integer.parseInt(ids[j]);
                if (idKey < 0 || idKey > 99) {
                    System.out.println("input error");
                    return;
                }
                int scoreValue = Integer.parseInt(scoreArray[j]);
                if (scoreValue < 0 || scoreValue > 100) {
                    System.out.println("input error");
                    return;
                }
                if (map.containsKey(idKey)) {
                    map.get(idKey).add(scoreValue);
                } else {
                    List<Integer> myList = new ArrayList<>();
                    myList.add(scoreValue);
                    map.put(idKey, myList);
                }
            }

            List<List<Integer>> allLists = new ArrayList<>();
            map.forEach((key, value) -> {
                List<Integer> dataList = new ArrayList<>();
                if (3 <= value.size()) {
                    Collections.sort(value);
                    dataList.add(key);
                    dataList.add(
                            value.get(value.size() - 1) + value.get(value.size() - 2) + value.get(value.size() - 3));
                }
                allLists.add(dataList);
            });
            // allLists.sort(new Comparator<List<Integer>>() {
            //     @Override
            //     public int compare(List<Integer> o1, List<Integer> o2) {
            //         if (o1.get(1) < o2.get(1)) {
            //             return o2.get(1) - o1.get(1);
            //         }
            //         if (o1.get(0) < o2.get(0)) {
            //             return o2.get(0)-o1.get(0);
            //         }
            //         return -1;
            //     }
            // });
            allLists.sort(new Comparator<List<Integer>>() {
                @Override
                public int compare(List<Integer> o1, List<Integer> o2) {
                    // 两个integer数超过128不可以用==比较
                    if (o1.get(1).equals(o2.get(1))) {
                        return o2.get(0) - o1.get(0);
                    } else {
                        return o2.get(1) - o1.get(1);
                    }
                }
            });

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < allLists.size() - 1; ++i) {
                sb.append(allLists.get(i).get(0)).append(",");
            }
            String result = sb.toString() + allLists.get(allLists.size() - 1).get(0);
            System.out.print(result);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("input error");
        }
    }
}
